var targetDom = null;
var currentList = [];// tchId:{iProdId:'',code:''}
$(function () {

    $('#ff').on('click',('.textbox-button'),function () {  //textBox按钮点击事件
        targetDom = $(this).parent().parent().find('.teacher');
        window.top.openScan();
    });


    $('#save').click(function () {
        // window.top.askDoSomething('确认','请确认信息填写完整',function () {
        window.top.askDelAction('确认','请确认信息填写完整',function () {
            var formData = $('#ff').serialize();
            var obj = {};
            obj.tchCode = currentList;
            formData = formData + '&' + $.param(obj);
            if (currentList.length != $('#iTchNum').numberbox('getText')) {
                window.top.sendErrorMsg('应出库的教师机数目不符');
                return false;
            }
            $.ajax({
                url: baseUrl + 'mobile/backlog/confirmApply',
                type: 'POST',
                data: formData,
                success: function (data) {
                    window.top.sendSuccessMsg(data);
                    window.top.upCountLog();
                },
                error: function (data) {
                    window.top.sendErrorMsg(data.responseText);
                }
            });
        });

    });
    $('#addShell').click(function () {
        var index = $('.modelTr').length  ;
        var dom = '<div class="modelTr" style="margin-bottom: 10px;height: 60px;">\n' +
            '        <div class="leftDiv">\n' +
            '            <select  class="easyui-combobox" name="shellCode['+index+'][prodId]" label="选择平板种类:" data-options="editable:false,labelPosition:\'top\'" style="width:100%">\n' +
            '                <option value="" style="display: none" selected>请选择</option>\n' ;
        $.each(tabletShell,function (id,text) {
            dom += '<option value="'+id+'">'+text+'</option>\n' ;
        });
        dom+='            </select>\n' +
            '        </div>\n' +
            '        <div class="rightDiv">\n' +
            '            <input  class="easyui-numberbox" label="平板壳数目:" name="shellCode['+index+'][iNum]" style="width:100%"\n' +
            '                   data-options="editable:true,labelPosition:\'top\'"/>\n' +
            '        </div>\n' +
            '    </div>';
        $('.modelTr:last').after(dom);
        $.parser.parse('.modelTr:last');
    });

});

function delTch(target) {
    window.top.swal({title:'警告',text: '请确认要删除这条记录嘛？',type:'info',showCancelButton:true,confirmButtonText:'确定',cancelButtonText:'取消'}).then(function () {
        var dom = $(target).parent().parent();
        $.each(currentList,function (i,item) {
           if(item['code'] == dom.attr('nodeId') && item['iProdId'] == dom.attr('prodId')){
               currentList.splice(i, 1);
               return false;//跳出循环
           }
        });
        dom.remove();
    },function () {
        return false
    });

}


/**
 * 扫码回调
 * @type {{setResult: JSObjectHandle.setResult}}
 * created by dale 2017/9/15
 */
var JSObjectHandle = {
    setResult: function (code) {
        setTimeout(function () {
            var iProdId = $('#prodId').combobox('getValue'),
                prodName = $('#prodId').combobox('getText'),
                maxNum = $('#iTchNum').numberbox('getValue');

            if(!iProdId ){
                window.top.sendErrorMsg('请选择平板类型');
                return false;
            }
            if (currentList.length >= maxNum){
                window.top.sendErrorMsg('已超过最大数量');
                return false;
            }
            var newObj  = {};
            newObj.code  = code;
            newObj.iProdId = iProdId;
            currentList.push(newObj);
            var str ='                      <tr nodeId="'+code+'" prodId="'+iProdId+'">\n' +
                '                            <td>'+prodName+'</td>\n' +
                '                            <td>'+code+'</td>\n' +
                '                            <td><a onclick="delTch(this)">删除</a></td>\n' +
                '                        </tr>';

            if($('#tchData tbody').find('tr').length <= 0){
                $('#tchData tbody').append(str);
            }else{
                $('#tchData tbody').find('tr').eq(0).before(str);
            }
            $('#code').textbox('setValue',code);
        }, 0);

    }
};